package com.golf.server.selection;

import java.util.HashMap;
import java.util.HashSet;

import com.golf.server.exceptions.AuthenticationFailureException;
import com.golf.server.exceptions.UserDuplicateException;

public class UserManager
{
	
	/** The singleton instance of the user manager. */
	private static final UserManager INSTANCE = new UserManager();
	
	
	private HashSet<User> users = new HashSet<User>();
	
	private HashMap<User, String> passwords = new HashMap<User, String>();
		
	/**
	 * Create the private, singleton <code>UserManager</code> instance. 
	 */
	private UserManager()
	{
		// Do nothing.
	}
	
	
	/**
	 * Get the singleton manager instance.
	 * 
	 * @return The singleton user manager instance.
	 */
	public static UserManager getInstance()
	{
		return INSTANCE;
	}
		
	
	
	public User authenticate(final String username, final String hash) throws AuthenticationFailureException
	{
		// TODO: AWFUL - FIX
		User tmp = new User(username);
		if (users.contains(tmp))
		{
			if (hash.equals(passwords.get(tmp)))
			{
				return tmp;
			}
		}
		
		throw new AuthenticationFailureException();
	}
	
	
	public User register(final String username, final String hash) throws UserDuplicateException
	{
		final User user = new User(username);
		if (users.contains(user))
		{
			throw new UserDuplicateException();
		}	
		
		users.add(user);
		passwords.put(user, hash);
		return user;
	}
	
	
	

}
